package com.zhang.controller.goods;

import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.goods.entity.Category;
import com.zhang.goods.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author 创世神
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/category/")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("add")
    R addCategory(@RequestBody Category category) {
        boolean save = categoryService.save(category);
        return save ? RUtils.successMsg("新增成功") : RUtils.err("新增失败");
    }

    @PostMapping("modify")
    R modifyCategory(@RequestBody Category category) {
        boolean modify = categoryService.updateById(category);
        return modify ? RUtils.successMsg("新增成功") : RUtils.err("新增失败");
    }

    @GetMapping("getAllToTree")
    R getCategoryToTree() {
        List<Category> list = categoryService.getTreeList();
        return RUtils.successData(list);
    }

    @GetMapping("get/list/by/parent/{parentId}")
    R getCategoryByParentId(@PathVariable Long parentId) {
        List<Category> list = categoryService.ListByParentId(parentId);
        return RUtils.successData(list);
    }

    /**获取一个分类下的所有子分类
     * @param parentId
     * @return {@link R}
     */
    @GetMapping("get/tree/list/by/parent/{parentId}")
    R getAllCategoryByParentId(@PathVariable Long parentId) {
        List<Category> list = categoryService.treeListByParentId(parentId);
        return RUtils.successData(list);
    }

    @DeleteMapping("del/{categoryId}")
    R delCategory(@PathVariable Long categoryId) {
        boolean b = categoryService.delCategory(categoryId);
        return b ? RUtils.successMsg("删除成功") : RUtils.err("删除失败");
    }
}
